/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InputFrame.java
 *
 * Created on Mar 17, 2010, 3:53:34 PM
 */

package gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.tinyos.mcenter.MessageCenter;
import net.tinyos.mcenter.MessageInjector;
import net.tinyos.message.Dump;
import net.tinyos.message.MessageFactory;
import net.tinyos.message.MoteIF;
//import net.tinyos.message.;
import net.tinyos.message.telos.TOSMsg;
import net.tinyos.packet.BuildSource;
import net.tinyos.packet.PacketSource;
import net.tinyos.tools.SimpleCmdMsg;
import net.tinyos.util.PrintStreamMessenger;

/**
 *
 * @author twentz
 */
public class InputFrame extends javax.swing.JInternalFrame {

    /** Creates new form InputFrame */
    public InputFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(159, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(153, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        //SimpleCmdMsg packet = new SimpleCmdMsg();
        
//        MessageCenter.main(null);
        //MessageCenter msginj = new MessageCenter();
       // msginj.pack();
       // msginj.setVisible(true);

        jTextArea1.setText("1");
        MoteIF mymote = null;
        TOSMsg packet = null;
PacketSource sfw = null;
SimpleCmdMsg packet2 = null;

        jTextArea1.setText("2");
try {
        packet2 = new SimpleCmdMsg();
        byte[] stuff = packet2.dataGet();
        for (int i = 0;i < stuff.length; ++i) {
            System.out.print(stuff[i]+" ");
        }
        System.out.println();


        jTextArea1.setText("3");
        byte sequenceNo = 0;
	sequenceNo = (byte) 9;
	packet2.set_seqno(sequenceNo);
	packet2.set_hop_count((short)0);
	packet2.set_source(0);

	    packet2.set_action((byte) 1);

//        try {
        String msg = "AABBCC";
        System.err.println("1");
        byte[] bytes = msg.getBytes();
        System.err.println("2");
        //packet = new TOSMsg(bytes);
        //packet.set_type((short)8);



        System.err.println("3");
        Process pmotelist = null, pserial = null;
//        String motecom = "";
//            try {
//                String syscomm = "";
//                //Linux
//                syscomm = "motelist -c";
//
//                // Windows
//                //syscomm = "C:/"
//                pmotelist = Runtime.getRuntime().exec("motelist -c");
//
//                BufferedReader buffread = new BufferedReader (
//                                            new InputStreamReader(pmotelist.getInputStream()));
//                String motelist = buffread.readLine();
//                String[] motes = motelist.split(",");
//
//                if (motes.length > 2) {
//                    motecom = motes[1];
//                }
//              //  System.getenv();
//
//                Map<String, String> env = System.getenv();
//        for (String envName : env.keySet()) {
//            System.out.format("%s=%s%n", envName, env.get(envName));
//        }
//        System.out.println("prop MOTECOM = "+System.getProperty("MOTECOM"));
//
//
// //       Runtime.getRuntime().exec("/usr/local/bin/unset MOTECOM");
//   //     Runtime.getRuntime().exec("export MOTECOM=serial@"+motecom+":tmote");
//        } catch (IOException ex) {
//                Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//        MessageFactory mf = new MessageFactory();
//        net.tinyos.message.TOSMsg tmg = mf.createTOSMsg(packet2.dataGet());



   //     PacketSource thingy = BuildSource.makeSF("localhost", 9001);


        jTextArea1.setText("5");
        sfw = BuildSource.makePacketSource();


        jTextArea1.setText("6");
        System.out.println(sfw.getName());
        sfw = BuildSource.makePacketSource("sf@localhost:9001");
        System.out.println(sfw.getName());


        jTextArea1.setText("7");
        } catch (Exception e) {
	     Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, e);
             JOptionPane.showMessageDialog(this, e.getMessage() + "\n" + e.getCause().toString());
	
        }


        //jTextArea1.setText("8");
        byte[] bpacket = null;

        try {
            sfw.open(PrintStreamMessenger.err);
        

	bpacket = packet2.dataGet();
	
	    sfw.writePacket(bpacket);
	}
	catch (IOException e) {
	     Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, e);
             JOptionPane.showMessageDialog(this, e.getMessage() + "\n" + e.getCause().toString());
	}
	Dump.printPacket(System.out, bpacket);
	System.out.println();

//        try {
//            //  mymote = new MoteIF(BuildSource.makeSF("localhost", 9001));
//            thingy.writePacket(packet2.dataGet());
//        } catch (IOException ex) {
//            Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        System.out.println(mymote.getName());
////        } catch (Exception iex) {
////            System.out.println(iex.getMessage());
////             Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, iex);
////        }
//        try {
//            mymote.send(MoteIF.TOS_BCAST_ADDR, packet2);
//        } catch (IOException ex) {
//            Logger.getLogger(InputFrame.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

}
